//
//  AfraDataBase--MovieActivity.m
//  UI_元旦豆瓣
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 31付冬梅. All rights reserved.
//

#import "AfraDataBase--MovieActivity.h"
#import "ActivityModel.h"
@implementation AfraDataBase__MovieActivity

+ (AfraDataBase__MovieActivity *)sharedInstenceDataBaseHandle {
    static AfraDataBase__MovieActivity *dataBaseActivity = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataBaseActivity = [[AfraDataBase__MovieActivity alloc] init];
    });
    return dataBaseActivity;
}

- (void)openDataBase {
    //获取到沙盒路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //拼接数据库路径
    NSString *dataBasePath = [document stringByAppendingPathComponent:@"AfraDataBase2016010901ActivityBase.sqlite"];
    NSLog(@"沙盒 ：%@", dataBasePath);
    
    //打开数据库
    int result = sqlite3_open(dataBasePath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库成功");
    } else {
        NSLog(@"打开数据库失败");
    }
    
    
}

- (void)createTable {
    //sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Activity(number integer primary key autoincrement, title text, begin_time text, end_time text, address text, participant_count ingeter, wisher_count ingeter, image text, content text, category_name text)"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
        
    } else {
        NSLog(@"建表失败");
    }
  
}

- (void)insertModel:(ActivityModel *)model {
    //sql语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Activity(title, begin_time, end_time, address, participant_count, wisher_count, image, content, category_name) values('%@', '%@', '%@', '%@', %@, %@, '%@', '%@', '%@')", model.title, model.begin_time, model.end_time, model.address, model.participant_count, model.wisher_count, model.image, model.content, model.category_name];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");

    } else {
        NSLog(@"添加失败");
    }
    
}

- (void)deleteAllModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Activity"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"清除成功");
    } else {
        NSLog(@"清除失败");
    }
    
}

- (void)deleteModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Activity where title = '%@'", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

- (NSMutableArray *)selectTable {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Activity"];
    //创建一个跟随指针，用来遍历数据库中的每一条数据
    sqlite3_stmt *stmt = nil;
    //进入查询
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    //创建一个数组，用来接收model
    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        //遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
//            const unsigned char *begin_time = sqlite3_column_text(stmt, 2);
//            const unsigned char *end_time = sqlite3_column_text(stmt, 3);
//            const unsigned char *address = sqlite3_column_text(stmt, 4);
//            integer_t participant_count = sqlite3_column_int(stmt, 5);
//            integer_t  wisher_count = sqlite3_column_int(stmt, 6);
            const unsigned char *image = sqlite3_column_text(stmt, 7);
//            const unsigned char *contant = sqlite3_column_text(stmt, 8);
//            const unsigned char *category_name = sqlite3_column_text(stmt, 9);
            //创建model 给model赋值
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
//            model.begin_time = [NSString stringWithUTF8String:(const char *)begin_time];
//            model.end_time = [NSString stringWithUTF8String:(const char *)end_time];
//            model.address = [NSString stringWithUTF8String:(const char *)address];
//            model.participant_count = [NSNumber numberWithInteger:participant_count];
//            model.wisher_count = [NSNumber numberWithInteger:wisher_count];
            model.image = [NSString stringWithUTF8String:(const char *)image];
//            model.content = [NSString stringWithUTF8String:(const char *)contant];
//            model.category_name = [NSString stringWithUTF8String:(const char *)category_name];
            [arrModel addObject:model];
            
            NSLog(@"%@", arrModel);
        }
    } else {
        NSLog(@"查询失败");
    }
    return arrModel;
    
}


@end
